package com.excemly.library.controller;

import com.excemly.library.common.ApiResponse;
import com.excemly.library.entity.Borrow;
import com.excemly.library.service.BorrowService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

//处理借阅记录接口，包含获取借阅记录、借阅图书和归还图书功能
@RestController
@RequestMapping("/api/borrows")
public class BorrowController {

    private final BorrowService borrowService;

    public BorrowController(BorrowService borrowService) {
        this.borrowService = borrowService;
    }

    @GetMapping
    public ApiResponse getUserBorrowRecords(@RequestParam Long userId) {
        List<Borrow> borrows = borrowService.getUserBorrowRecords(userId);
        Map<String, Object> data = new HashMap<>();
        data.put("data", borrows);
        return ApiResponse.success("获取借阅记录成功", data);
    }

    @PostMapping
    public ApiResponse borrowBook(@RequestParam Long bookId, @RequestParam Long userId) {
        Borrow borrow = borrowService.borrowBook(bookId, userId);
        Map<String, Object> data = new HashMap<>();
        data.put("data", borrow);
        return ApiResponse.success("借阅成功", data);
    }

    @PutMapping("/return")
    public ApiResponse returnBook(@RequestParam Long borrowId) {
        borrowService.returnBook(borrowId);
        Map<String, Object> data = new HashMap<>();
        data.put("data", null);
        return ApiResponse.success("归还成功", data);
    }
}